﻿using Engine.Graphics;
using ModelViewer;
using SharpDX;
using System;
using System.Windows.Forms;

namespace ModelViewerRoot.TabsForModelviewer
{
    public partial class PaintMaterial : UserControl
    {
        #region Private Fields

        private Engine.Engine engine;
        private MaterialManager materialManager;
        private SelectedObject model;

        #endregion Private Fields

        #region Public Constructors

        public PaintMaterial()
        {
            InitializeComponent();

            this.comboBox_CurrentMappingMode.DataSource = Enum.GetValues(typeof(EnumMappingMode));
        }

        #endregion Public Constructors

        #region Public Methods

        public void SaveCurrentMaterialToXML(string filename)
        {
            this.materialPainter1.SaveCurrentMaterialToXML(filename);
        }

        #endregion Public Methods

        #region Internal Methods

        internal void SetGraphicEngine(Engine.Engine engine)
        {
            this.engine = engine;
        }

        // Internal Methods (5) 
        internal void SetMaterial(Material material)
        {
            this.comboBox_CurrentMappingMode.SelectedItem = material.MappingMode;
            this.materialPainter1.SetMaterial(material, null);
        }

        internal void SetMaterialManager(MaterialManager materialManager)
        {
            this.materialManager = materialManager;
            this.textureListBox.Items.Clear();
            this.materialPainter1.MaterialManager = this.materialManager;
            this.materialPainter1.SetDevice(this.materialManager);

            for (int i = 0; i < this.materialManager.TextureNames.Count; i++)
            {
                this.textureListBox.Items.Add(this.materialManager.TextureNames[i]);
            }

            if (this.textureListBox.Items.Count > 0)
                this.textureListBox.SelectedIndex = 0;
        }

        internal void SetModel(SelectedObject model)
        {
            this.model = model;
        }

        internal void UseTool()
        {
            if (this.model != null)
                if (this.model.ScenerieObject.Mesh != null)
                {
                    Vector3 brushPosition;
                    int materialID;
                    int intersectedPolygon;
                    this.model.ScenerieObject.Mesh.Intersect(this.engine.Grafic.VNear, this.engine.Grafic.VFar, out brushPosition, out materialID, out intersectedPolygon);

                    if (this.materialPainter1.checkBox_PaintVertexColor.Checked)
                    {
                        if (brushPosition != Vector3.Zero && this.engine.Mouse.ButtonL)
                        {
                            //this.model.ScenerieObject.Mesh.PaintVertexColor(brushPosition, materialID, this.materialPainter1.VertexColor, 30);
                            this.model.ScenerieObject.Mesh.PaintNearestVertex(brushPosition, this.materialPainter1.VertexColor, intersectedPolygon);
                        }
                        this.label_MaterialID.Text = string.Format("Material: {0}", materialID);
                    }

                    if (this.materialPainter1.checkBox_Material.Checked)
                    {
                        if (brushPosition != Vector3.Zero && this.engine.Mouse.ButtonL)
                        {
                            this.model.ScenerieObject.Mesh.PaintMaterial(intersectedPolygon, this.materialPainter1.GetMaterial());
                        }
                    }
                }
        }

        #endregion Internal Methods

        #region Private Methods

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.materialPainter1.CurrentMappingMode = (EnumMappingMode)this.comboBox_CurrentMappingMode.SelectedValue;
        }

        private void textureListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.textureListBox.SelectedIndex > -1)
            {
                this.materialPainter1.SetTexture(this.textureListBox.SelectedIndex);
            }
        }

        #endregion Private Methods
    }
}